pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let mut ch_it = num_str.chars().rev();

    // parse the value of num_str
    let num = parse_base(&mut ch_it);
    // println!("num: {num}");

    generate_num_str(num, to_base)
}

/**
 * Parse the "(base)" at the end of `ch_it`.
 * 
 * # Return
 * 
 * value of the base.
 */
fn parse_base(ch_it: &mut impl Iterator<Item = char>) -> u32 {
    
    let rb = ch_it.next().unwrap();
    assert_eq!(rb, ')');
    
    // parse the original base
    let base = parse_num(ch_it, 10);
    // println!("base: {base}");

    // then parse the value of `num_str`
    parse_num(ch_it, base)
}

/**
 * Parse a number in base `base` at the end of `ch_it`.
 * End when hit char that is not number, actually, `None` or `(`.
 * 
 * # Return
 * 
 * Value of the number.
 */
fn parse_num(ch_it: &mut impl Iterator<Item = char>, base: u32) -> u32 {
    let mut ans = 0;
    let mut multiper = 1; // 记录数位
    loop {
        match ch_it.next() {
            Some(ch) if ch >= '0' && ch <= '9' => {
                let v = ch as u32 - '0' as u32;
                ans += v * multiper;
                multiper *= base;
                // println!("ch: {ch}, ans = {ans}");
            }
            _ => {
                return ans;
            }
        }
    }
}

/**
 * Generate the string of the `num` in base `base`.
 * 
 * # Return
 * 
 * String result.
 */
fn generate_num_str(mut num: u32, to_base: u32) -> String {
    assert!(to_base >= 2 && to_base <= 16);
    let mut res = Vec::new();
    while num != 0 {
        let cur = num % to_base;
        num /= to_base;

        // trans into alpha if larger than 9
        let ch = if cur <= 9 {
            (b'0' + cur as u8) as char
        } else {
            (b'a' + (cur as u8 - 10)) as char
        };
        res.push(ch);
    }
    res.into_iter().rev().collect()
}
